package cn.marinda.gui.impl;

import cn.marinda.entity.AbstractServerCore;
import cn.marinda.entity.AbstractServerPlugin;
import cn.marinda.entity.ComponentSource;
import cn.marinda.gui.AbstractHelper;
import cn.marinda.listener.*;
import cn.marinda.util.HttpUtils;
import cn.marinda.util.ServerUtils;
import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * 创建服务端界面
 *
 * @author marinda
 * @date 2022/07/13
 */
@Data
public class OnlinePluginFilesHelperImpl extends AbstractHelper {

    public static OnlinePluginFilesHelperImpl instance = new OnlinePluginFilesHelperImpl(HttpUtils.loadOnlinePluginFiles());
    /**
     * 创建服务端界面
     */
    private OnlinePluginFilesHelperImpl(List<String> onlineList){
        super(onlineList);
    }

    @Override
    protected JFrame loadFrame() {
        frame = new JFrame();
        frame.setSize(700,500);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setTitle("在线服务");
        frame.setJMenuBar(menuBar);
        frame.setContentPane(view);
        frame.pack();
        return frame;
    }


    @Override
    protected JPanel loadPanel() {
        view = new JPanel();
        Box boxIndex1 = Box.createHorizontalBox();
        Box boxIndex2 = Box.createVerticalBox();
        Box boxIndex3 = Box.createVerticalBox();
        JLabel label = createTextComponent("在线插件列表");
        boxIndex1.add(Box.createHorizontalStrut(200));
        boxIndex1.add(label);
        boxIndex1.add(Box.createHorizontalGlue());
        boxIndex1.add(Box.createVerticalStrut(50));
        for(Component component : componentList){
            if(component instanceof JList){
                JList list = (JList) component;
                list.addListSelectionListener(new PluginListSelectionEvent(list));
                boxIndex2.add(Box.createHorizontalStrut(500));
                boxIndex2.add(list);
                boxIndex2.add(Box.createVerticalStrut(20));
                continue;
            }
            if(component instanceof  JButton){
                JButton btn = (JButton) component;
                btn.addActionListener(new DownloadOnlinePluginEvent(ServerUtils.serverPluginSet,btn));
                btn.setForeground(Color.black);
                btn.setBackground(Color.RED);
                boxIndex3.add(Box.createHorizontalStrut(300));
                boxIndex3.add(component);
                boxIndex3.add(Box.createVerticalStrut(20));
                continue;
            }
            boxIndex3.add(Box.createHorizontalStrut(250));
            boxIndex3.add(component);
        }
        boxPanel.add(boxIndex1);
        boxPanel.add(boxIndex2);
        boxPanel.add(boxIndex3);
        view.add(boxPanel);
        return view;
    }

    @Override
    protected List<Component> loadComponent() {
        JList<String> list = createListComponent(onlineList.toArray());
        JButton downloadBtn = createButtonComponent("立即下载");
        JLabel tips = createTextComponent("想分享插件？联系作者并发送插件，审核通过后即可");
        tips.setForeground(Color.red);
        tips.setFont(new Font(null,Font.PLAIN,12));
        componentList.add(list);
        componentList.add(downloadBtn);
        componentList.add(tips);
        return componentList;
    }

}